package ws

import (
	"encoding/json"
	"fmt"

	. "starsine.cn/appTpl/cmn/defs"
	"starsine.cn/appTpl/cmn/logs"
)

var wsClient *WsClient = nil

func Start(appCtxt *AppContext) {
	wsClient = CreateWsClient(appCtxt)
	wsClient.Start()

	logs.Infof("registry..wsclient start")
}

func WriteMessage(msg []byte) error {
	return wsClient.writeMessage(msg)
}

func SendMessage(v interface{}) error {
	d, err := json.Marshal(v)
	if err != nil {
		logs.Errorf("wsClient.SendMessage to Marshal fail: %+v", v)
		return fmt.Errorf("wsClient.SendMessage to Marshal fail: %+v", v)
	}
	return wsClient.writeMessage(d)
}

func RegisterProc(fn func([]byte)) {
	wsClient.registerProc(fn)
}
